home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / mflms101.arc / WC.C < prev   
C/C++ Source or Header  |  1989-11-25  |  6KB  |  186 lines

  1. /*
  2. **                WORD COUNT UTILITY
  3. **
  4. ** syntax:
  5. ** wc [-cwlsv] file
  6. ** -c count only characters
  7. ** -w count only words
  8. ** -l count only lines
  9. ** -s gives only a hex checksum
  10. ** -q silences reporting of filenames
  11. ** file is input file (Do NOT use stdin redirection!)
  12. ** options must be first argument only
  13. ** wildcard filenames are allowed
  14. **
  15. ** ========== COPYRIGHT 1988 BY STEVEN E. MARGISON ==============
  16. ** 11-08-88 A  Turbo-C 2.0
  17. **
  18. **   Modified 1989 by Bob Stout
  19. **
  20. **   As distributed, this program requires (for compilation):
  21. **     "The MicroFirm Function LIbrary for MS/QC"
  22. **   which may be obtained without registration from many Bulletin
  23. **   Board Systems.
  24. **
  25. **   or by registration:
  26. **      $25 for Docs, C, S, M, L, H libraries, and complete library source
  27. **              in C and Assembler
  28. **     MicroFirm
  29. **     P.O. Box 428
  30. **     Alief, TX 77411
  31. **
  32. **
  33. */
  34.  
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include <mflfiles.h>
  38.  
  39. int binflg, copt, wopt, lopt, sopt, allopt, verbose;
  40. FILE *fd;
  41. int nargc;
  42.  
  43. main(argc, argv)
  44. int argc;
  45. char *argv[];
  46. {
  47.         char *cksum, *nc, *nl, *nw;
  48.         int index, c, inword, argci;
  49.  
  50.         if (argc < 2)
  51.                 usage();
  52.  
  53.         nargc = expand_args(argc, argv);  /* expand wildcards */
  54.         if (nargc == 0) error("Error in command line expansion");
  55.  
  56.         verbose = TRUE;
  57.         sopt = lopt = wopt = copt = FALSE;
  58.         allopt=TRUE;
  59.         argci = 1;
  60.  
  61.         if (nargv[argci][0] == '-')
  62.         {
  63.                 index = 0;
  64.                 while (nargv[argci][++index] != NUL)
  65.                 {
  66.                         switch(tolower(nargv[argci][index]))
  67.                         {
  68.                         case 'q':
  69.                                 verbose = FALSE;
  70.                                 break;
  71.                         case 'c':
  72.                                 copt = TRUE;
  73.                                 allopt = FALSE;
  74.                                 break;
  75.                         case 'w':
  76.                                 wopt = TRUE;
  77.                                 allopt = FALSE;
  78.                                 break;
  79.                         case 'l':
  80.                                 lopt = TRUE;
  81.                                 allopt = FALSE;
  82.                                 break;
  83.                         case 's':
  84.                                 sopt = TRUE;
  85.                                 allopt = FALSE;
  86.                                 break;
  87.                         default:
  88.                                 usage();
  89.                         } /* end of switch */
  90.                 } /* end of while */
  91.                 argci++;    /* get around options */
  92.         } /* end of option "if" */
  93.  
  94.         while (argci < nargc)
  95.         {
  96.                 cksum = nc = nl = nw = 0;
  97.                 inword = FALSE;
  98.                 if (nargv[argci][0] == '-')
  99.                         usage();
  100.  
  101.                 do_open(nargv[argci++]);
  102.  
  103.                 while ((c = fgetc(fd)) != EOF)
  104.                 {
  105.                         cksum += c;
  106.                         if (binflg)
  107.                                 continue;
  108.                         if (c == '\n')
  109.                         {
  110.                                 ++nl;
  111.                                 ++nc;
  112.                                 ++nc;     /* because a newline is actually two characters */
  113.                                 inword = FALSE;
  114.                         }
  115.                         else    ++nc;
  116.                         if (isspace(c))
  117.                                 inword = FALSE;
  118.                         else if (inword == FALSE)
  119.                         {
  120.                                 inword = TRUE;
  121.                                 ++nw;
  122.                         }
  123.                 }  /* end of inner while */
  124.                 if (allopt && !binflg)
  125.                         printf("%6d characters  %d words  %d lines  %6x checksum\n",
  126.                                 nc, nw, nl, cksum);
  127.                 if (binflg)
  128.                         printf("%x checksum\n", cksum);
  129.  
  130.                 if (copt)
  131.                         printf("%d\n", nc);
  132.                 if (wopt)
  133.                         printf("%d\n", nw);
  134.                 if (lopt)
  135.                         printf("%d\n", nl);
  136.                 if (sopt)
  137.                         printf("%x\n", cksum);
  138.                 fclose(fd);
  139.         } /* end of file loop */
  140. }
  141.  
  142. usage()
  143. {
  144.         fputs("WC Version 1.51 11-08-88  Copyright 1988 S.E. Margison\n", stderr);
  145.         error("usage: wc [-cwlsq] <file1, file2, filen>");
  146. }
  147.  
  148. do_open(char *string)
  149. {
  150.         binflg = FALSE;
  151.  
  152.         if (exttyp(string, "OBJ") == TRUE)
  153.         {
  154.                 binflg = TRUE;
  155.                 goto AA;
  156.         }
  157.         if (exttyp(string, "EXE") == TRUE)
  158.         {
  159.                 binflg = TRUE;
  160.                 goto AA;
  161.         }
  162.         if (exttyp(string, "COM") == TRUE)
  163.         {
  164.                 binflg = TRUE;
  165.                 goto AA;
  166.         }
  167.         if (exttyp(string, "ARC") == TRUE)
  168.         {
  169.                 binflg = TRUE;
  170.                 goto AA;
  171.         }
  172.         if (exttyp(string, "LIB") == TRUE)
  173.                 binflg = TRUE;
  174. AA:     if (!binflg)
  175.         {
  176.                 if ((fd = fopen(string, "r")) == NULL)
  177.                         cant(string);
  178.         }
  179.         else
  180.         {
  181.                 if ((fd = fopen(string, "rb")) == NULL)
  182.                         cant(string);
  183.         }
  184.         if (verbose) printf("File: %s\n", string);
  185. }
  186.